<template>
  <el-card style="height: 80px">
    <el-form :inline="true" class="form">
      <el-form-item label="用户名:">
        <el-input placeholder="请输入用户名" v-model.trim="keyword"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="default" :disabled="!keyword" @click="search">搜索</el-button>
        <el-button type="primary" size="default" @click="reset">重置</el-button>
      </el-form-item>
    </el-form>
  </el-card>
  <el-card style="margin: 10px 0">
    <el-button type="primary" size="default" @click="addUser">添加用户</el-button>
    <el-button type="primary" size="default" @click="deleteSelectUser">批量删除</el-button>
    <el-table @selection-change="selectChange" style="margin: 10px 0" border :data="tableData" v-loading="loading">
      <el-table-column type="selection" align="center"></el-table-column>
      <el-table-column label="#" align="center" type="index"></el-table-column>
      <el-table-column label="ID" align="center" prop="id"></el-table-column>
      <el-table-column label="用户名字" align="center" prop="username" show-overflow-tooltip></el-table-column>
      <el-table-column label="用户名称" align="center" prop="name" show-overflow-tooltip></el-table-column>
      <el-table-column label="用户角色" align="center" prop="roleName" show-overflow-tooltip></el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" show-overflow-tooltip></el-table-column>
      <el-table-column label="更新时间" align="center" prop="updateTime" show-overflow-tooltip></el-table-column>
      <el-table-column label="操作" width="300px" align="center">
        <template #default="{ row }">
          <el-button type="primary" size="small" icon="User" @click="setRole(row)">分配角色</el-button>
          <el-button type="primary" size="small" icon="Edit" @click="updateUser(row)">编辑</el-button>
          <el-popconfirm :title="`你确定要删除${row.username}?`" width="260px" @confirm="deleteUser(row.id)">
            <template #reference>
              <el-button type="primary" size="small" icon="Delete">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页器 -->
    <el-pagination
      v-model:current-page="currentPage"
      v-model:page-size="pageSize"
      :page-sizes="[5, 7, 9, 11]"
      :background="true"
      layout="prev, pager, next, jumper,->,sizes,total"
      :total="total"
      @current-change="changeCurrentPage"
      @size-change="changePageSize"
    />
  </el-card>
  <userActionsLog
    ref="userActionsLogRef"
    :modalData="modalData"
    :modalType="modalType"
    @undated="handlerUndated"
  ></userActionsLog>
  <assignRolesLog ref="assignRolesLogRef" :modalData="modalData" @undated="handlerUndated"></assignRolesLog>
</template>

<script setup lang="ts">
import { onMounted, ref } from 'vue'
import usePagination from '@/hooks/pagination'
import useSearch from '@/hooks/search'
import userActionsLog from './userActionsLog/index.vue'
import assignRolesLog from './assignRolesLog/index.vue'
import { reqUserInfo } from '@/api/acl/user'

let loading = ref<boolean>(false)
let modalType = ref<string>('')
let userActionsLogRef = ref<any>()
let assignRolesLogRef = ref()
//存储全部用户的数组
let tableData = ref<any[]>([])
const modalData = ref<any>({})

onMounted(() => {
  getTableData()
})
const { keyword, search, reset } = useSearch(() => getTableData())
const { currentPage, pageSize, total, changePageSize, changeCurrentPage } = usePagination(() => getTableData(), {
  defaultPageSize: 5
})
//获取全部已有的用户信息
const getTableData = async () => {
  loading.value = true
  let result: any = await reqUserInfo(currentPage.value, pageSize.value, keyword.value)
  loading.value = false
  if (result.code === 200) {
    total.value = result.data.total
    tableData.value = result.data.records
  }
}
const handlerUndated = () => {
  getTableData()
}
const addUser = () => {
  modalType.value = 'add'
  modalData.value = null
  userActionsLogRef.value.open()
}
const updateUser = (val: any) => {
  modalType.value = 'edit'
  modalData.value = val
  userActionsLogRef.value.open()
}
const deleteSelectUser = () => {}
const selectChange = (val: any) => {
  console.log(val)
}
const setRole = (val: any) => {
  modalData.value = val
  modalType.value = 'add'
  assignRolesLogRef.value.open()
}
const deleteUser = (val: any) => {
  console.log(val)
}
</script>

<style scoped>
.form {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
